<?php

class Upload extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		$this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload()
	{
		$this->db->select_max('id_neraca','max_id');
		$id_la = $this->db->get('db_neraca');
		foreach ($id_la->result() as $id_ba){
			$id_lam = $id_ba->max_id;
		}
		if ( $id_lam==0 ){
			$id_bar = 10001;
		}
		else {
			$id_bar = $id_lam+1;
		}
		
		$wakt_neraca = date('Y-m-d');
		$this->db->where('waktu_neraca', $wakt_neraca);
		$hasil = $this->db->get('db_neraca');
		$ind = (int) $hasil->num_rows;
		if ( $ind==0 ){
			$ind1 = 01;
		}
		else {
			$ind1 = $ind+1;
		}
		$ind12 = (String) $ind1;
		$wakt_neraca1 = (String) $wakt_neraca;
		$nama_file = $wakt_neraca1+"_"+$ind12;
		
		$config['upload_path'] = './file/';
		$config['allowed_types'] = 'gif|jpg|png|xlsx|xls';
		$config['max_size']	= '102400';
		$config['file_name'] = $nama_file;//Belum sesuai

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());

			$this->load->view('upload_form', $error);
		}
		else
		{
			$data1 = array('upload_data' => $this->upload->data());
			$datafile = $this->upload->data();
			$fileneraca = $datafile['full_path'];
			$data = array(
				'id_neraca' => $id_bar,
				'fileneraca' => $fileneraca,
				'waktu_neraca' => $wakt_neraca,
				'nama_neraca' => $nama_file
				);
			$this->db->insert('db_neraca',$data);
			//$error['error'] = "File Berhasil disimpan";
			$this->load->view('upload_success', $data1);
		}
	}
}
?>